<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增正选</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="学期" align="center" prop="batchName" />
      <el-table-column label="开始时间" align="center" prop="beginDate" />
      <el-table-column label="结束时间" align="center" prop="endDate" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleupdate(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 开始正选选择时间 -->
    <el-dialog
      :title="title"
      :visible.sync="curriculaVariableOpen"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="700px"
      append-to-body
      @close="cancel()"
    >
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <div class="flex">
          <el-form-item label="起止日期" prop="beginDate">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptionsEnd"
              v-model="form.beginDate"
              type="date"
              clearable
              placeholder="开始日期"
            >
            </el-date-picker>
          </el-form-item>
          <span style="margin: 7px 10px 0">至</span>
          <el-form-item prop="endDate" label-width="0px">
            <el-date-picker
              :picker-options="pickerOptionsStart"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="form.endDate"
              type="date"
              clearable
              placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="curriculaVariableOpen = false"
          >取 消</el-button
        >
        <el-button v-preventReClick type="primary" @click="submitForm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  selectionlist,
  addPost_selection,
  updatePost_selection,
} from "@/api/course/curriculaVariable";

export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      title: "开始正选",
      pickerOptionsStart: {
        // 时间只能大于startTime
        disabledDate: (time) => {
          if (!this.form.beginDate) {
            return;
          }
          return time.getTime() < new Date(this.form.beginDate).getTime();
        },
      },
      pickerOptionsEnd: {
        // 时间只能小于startTime
        disabledDate: (time) => {
          if (!this.form.endDate) {
            return;
          }
          return time.getTime() > new Date(this.form.endDate).getTime();
        },
      },
      curriculaVariableOpen: false,
      form: {
        beginDate: undefined,
        endDate: undefined,
      },
      // 表单校验
      rules: {
        beginDate: [
          { required: true, message: "时间不能为空", trigger: "blur" },
        ],
        endDate: [{ required: true, message: "时间不能为空", trigger: "blur" }],
      },
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 岗位表格数据
      postList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      selectionlist().then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.curriculaVariableOpen = true;
      this.title = "添加正选";
    },
    handleupdate(item) {
      this.curriculaVariableOpen = true;
      this.title = "编辑正选";
      this.form = {...item};
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.title == "添加正选") {
            await addPost_selection({
              beginDate: this.form.beginDate,
              endDate: this.form.endDate,
              preStatus: 2,
              realStatus: 1,
              schoolId: this.$store.getters.schoolId,
              temporaryStatus: 1,
            });
            this.$modal.msgSuccess("添加正选成功");
          } else {
            await updatePost_selection(this.form);
            this.$modal.msgSuccess("修改正选成功");
          }
          this.getList();
          this.curriculaVariableOpen = false;
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
</style>
